package com.epam.web_project.bank.command.show_commands;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;

import com.epam.web_project.bank.command.Command;
import com.epam.web_project.bank.dao.customer_dao.CustomerDAO;
import com.epam.web_project.bank.dao.factory.MySQLDAOFactory;
import com.epam.web_project.bank.db.ConnectionPool;
import com.epam.web_project.bank.entity.Customer;
import com.epam.web_project.bank.util.AppPropertiesLoader;

public class ShowAdminMainCommand implements Command {

	private static Logger logger=Logger.getLogger(ShowAdminMainCommand.class);
	//show admin main page
	@Override
	public String execute(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession();
	//	if(!session.isNew()) {
			MySQLDAOFactory daoFactory = new MySQLDAOFactory();
			daoFactory.setConnectionPool(ConnectionPool.getInstance());
			CustomerDAO customerDAO=daoFactory.getCustomerDAO();
			List<Customer> customersUnapprovedAcc=new ArrayList<Customer>();
			List<Customer> unapprovedCustomers=new ArrayList<Customer>();
			customersUnapprovedAcc = customerDAO.findByUnapprovedAcc();
			unapprovedCustomers=customerDAO.findUnapprovedCustomers();
			String message=(String)session.getAttribute("message");
			request.setAttribute("message", message);
			session.removeAttribute("message");
			if(customersUnapprovedAcc!=null && !customersUnapprovedAcc.isEmpty()) {
				request.setAttribute("customersUnapprovedAcc", customersUnapprovedAcc);
			}
			if(unapprovedCustomers!=null && !unapprovedCustomers.isEmpty()) {
				request.setAttribute("unapprovedCustomers", unapprovedCustomers);
			}
//		}
		AppPropertiesLoader props = AppPropertiesLoader.getInstance();
		return props.getProperty("admin_main_page");
	}

}
